import { Form } from '@nutui/nutui-react-taro';
import { SocialCodeReg } from '@/common/reg';
import RadioGroup from '../RadioGroup';
import {
  DepartmentDmEnum,
  OrgTypeDmEnum,
  enumConvertOption,
  whether,
} from '@/common/enum';
import Input from '@/components/Input';
import Picker from '@/components/Picker';
import Cascader from '../Cascader';
import { FormInstance } from '@nutui/nutui-react-taro/dist/types/packages/form/types';
import { getCurrentAddressListThree } from '@/services/systemAddress';
import { useEffect, useState } from 'react';
import { useRequest } from 'taro-hooks';
import { generateCreditCode } from '@/services/system';
import { Item } from '@/components/Descriptions';

export default ({
  form,
  fieldNames = { value: 'sfsytyshxydm', code: 'tyshxxdm' },
}: {
  form?: FormInstance;
  fieldNames?: {
    code: string;
    value: string;
  };
}) => {
  const [data, setData] = useState<any>({});

  const formData = form?.getFieldsValue(['djglbm', fieldNames.value]) || {};

  const { run } = useRequest(generateCreditCode, {
    manual: true,
    onSuccess(v) {
      form?.setFieldsValue({ [fieldNames.code]: v });
    },
  });

  const change = () => {
    setTimeout(() => {
      const { djglbm, djbmssqh, jglb } =
        form?.getFieldsValue(['djglbm', 'djbmssqh', 'jglb']) || {};
      if (djglbm && djbmssqh?.length && jglb) {
        run({ djglbm, djbmssqh: djbmssqh?.at?.(-1)?.slice(0, 6), jglb });
      }
      setData({ djglbm, djbmssqh, jglb });
    }, 100);
  };

  useEffect(() => {
    setData({
      ...data,
      djglbm: formData.djglbm,
      [fieldNames.value]: formData?.[fieldNames.value],
    });
  }, [formData?.[fieldNames.value]]);
  return (
    <>
      <Form.Item
        required
        label="是否统一社会信用代码"
        name={fieldNames.value}
        initialValue={0}
      >
        <RadioGroup
          options={whether}
          onChange={(v) => setData({ ...data, [fieldNames.value]: v })}
        />
      </Form.Item>

      {data?.[fieldNames.value] !== 1 && (
        <>
          <Form.Item
            name="djglbm"
            label="登记管理部门"
            rules={[
              {
                required: true,
                message: '请选择',
              },
            ]}
          >
            <Picker
              options={enumConvertOption(DepartmentDmEnum)}
              onChange={() => {
                form?.setFieldsValue({ jglb: undefined });
                change();
              }}
            />
          </Form.Item>
          <Form.Item
            name="djbmssqh"
            label="登记管理部门所在行政区划"
            rules={[
              {
                required: true,
                message: '请选择',
              },
            ]}
          >
            <Cascader
              customLeaf={(item) => !item?.zjd?.length}
              lazyApi={getCurrentAddressListThree}
              onChange={change}
              fieldNames={{
                label: 'qhmc',
                value: 'qhdm',
                children: 'zjd',
              }}
            />
          </Form.Item>

          <Form.Item
            name="jglb"
            label="机构类别"
            rules={[
              {
                required: true,
                message: '请选择',
              },
            ]}
          >
            <Picker
              options={enumConvertOption(OrgTypeDmEnum[data.djglbm] || {})}
              onChange={change}
            />
          </Form.Item>
        </>
      )}
      <Form.Item
        required
        label="统一社会信用代码"
        name={fieldNames.code}
        rules={[
          { required: true, message: '请输入统一社会信用代码' },
          {
            validator: (_ruleCfg, v) => SocialCodeReg.test(v),
            message: '输入的代码不正确',
          },
        ]}
      >
        <Input
          placeholder="请填写统一社会信用代码"
          readOnly={data?.[fieldNames.value] !== 1}
        />
      </Form.Item>
    </>
  );
};

export const UnifiedCreditCodeDetail = ({
  fieldNames = { value: 'sfsytyshxydm', code: 'tyshxxdm' },
  signs,
  data,
}: {
  fieldNames?: {
    code: string;
    value: string;
  };
  signs?: string[];
  data?: any;
}) => {
  return (
    <>
      <Item
        label="是否统一社会信用代码"
        content={data?.[fieldNames.value] === 1 ? '是' : '否'}
        sign={signs?.includes('sfsytyshxydm')}
      />
      {data?.[fieldNames.value] !== 1 && (
        <>
          <Item
            label="登记管理部门"
            content={data?.djglbm}
            sign={signs?.includes('djglbm')}
          />
          <Item
            label="登记管理部门所在行政区划"
            content={data?.djbmssqhmc}
            sign={signs?.includes('djbmssqhmc')}
          />
          <Item
            label="机构类别"
            content={OrgTypeDmEnum?.[data?.djglbm]?.[data?.jglb]}
            sign={signs?.includes('jglb')}
          />
        </>
      )}
      <Item
        label="统一社会信信用代码"
        content={data?.[fieldNames.code]}
        sign={signs?.includes(fieldNames.code)}
      />
    </>
  );
};
